package cn.com.ghy.android.widget.visit;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import cn.com.ghy.android.bean.DataCollectDayInfoBean;
import cn.com.ghy.android.bean.UnitInfoBean;
import cn.com.ghy.android.util.CommonUtil;
import cn.com.ghy.android.widget.R;

public class DataCollectDay extends Activity {
	private EditText txtSku = null;
	private EditText txtPrice = null;
	private EditText txtPaiMian = null;
	private EditText txtCommonts = null;
	private Spinner spiUnit = null;
	private Button btnSave = null;
	private Button btnBack = null;
	private Intent intent = null;
	private Bundle bundle = null;

	private List<UnitInfoBean> unitInfoList = null;
	private DataCollectDayInfoBean dataCollectDayInfo = null;
	private int dataCollDayPosition = -1;
	private List<DataCollectDayInfoBean> dataCollectDayInfoList = null;
	private String status = "add";
	private String oldSKU = null;

	@SuppressWarnings("unused")
	private static final String TAG = "DataCollectDay";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.data_collect_day);
		getSession();
		findView();
		init();
		addListener();
	}

	@SuppressWarnings("unchecked")
	private void getSession() {
		bundle = this.getIntent().getExtras();
		unitInfoList = (List<UnitInfoBean>) bundle.getSerializable("unitInfoList");
		dataCollectDayInfo = (DataCollectDayInfoBean) bundle.getSerializable("dataCollectDayInfo");
		dataCollDayPosition = bundle.getInt("dataCollDayPosition", -1);
		dataCollectDayInfoList = (List<DataCollectDayInfoBean>) bundle
				.getSerializable("dataCollectDayInfoList");
	}

	private void findView() {
		txtSku = (EditText) this.findViewById(R.id.txtSku);
		txtPrice = (EditText) this.findViewById(R.id.txtPrice);
		txtPaiMian = (EditText) this.findViewById(R.id.txtPaiMian);
		txtCommonts = (EditText) this.findViewById(R.id.txtCommonts);
		spiUnit = (Spinner) this.findViewById(R.id.spiUnit);
		btnSave = (Button) this.findViewById(R.id.btnSave);
		btnBack = (Button) findViewById(R.id.btnBack);
	}

	private void init() {
		ArrayAdapter<UnitInfoBean> unitInfoAdapter = new ArrayAdapter<UnitInfoBean>(
				DataCollectDay.this, android.R.layout.simple_spinner_item, unitInfoList);
		unitInfoAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spiUnit.setAdapter(unitInfoAdapter);
		if (null != dataCollectDayInfo) {
			oldSKU = dataCollectDayInfo.getBismt();
			txtSku.setText(dataCollectDayInfo.getBismt());
			txtPrice.setText(dataCollectDayInfo.getSaleprice());
			txtPaiMian.setText(dataCollectDayInfo.getSide());
			txtCommonts.setText(dataCollectDayInfo.getRemark());
			spiUnit.setSelection(getSpinnerSelectedItemPosition(dataCollectDayInfo.getUnitInfo()),
					true);
			status = "modify";
		} else {
			dataCollectDayInfo = new DataCollectDayInfoBean();
			status = "add";
		}
	}

	private int getSpinnerSelectedItemPosition(UnitInfoBean unitInfo) {
		int position = 0;
		for (int i = 0; i < unitInfoList.size(); i++) {
			if (unitInfo.getUnit().equals(unitInfoList.get(i).getUnit())) {
				position = i;
			}
		}
		return position;
	}

	private void addListener() {
		txtSku.setKeyListener(CommonUtil.charNumListener);
		txtSku.setOnFocusChangeListener(new OnFocusChangeListener() {
			public void onFocusChange(View v, boolean hasFocus) {
				String str = ((EditText) v).getText().toString();
				if (!hasFocus && !"".equals(str)) {
					txtSku.setText(str.toUpperCase());
				}
			}
		});

		spiUnit.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				UnitInfoBean bean = (UnitInfoBean) parent.getItemAtPosition(position);
				dataCollectDayInfo.setUnitInfo(bean);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}

		});

		btnSave.setOnClickListener(btnListener);
		btnBack.setOnClickListener(btnListener);
	}

	private boolean getData() {
		String skuStr = txtSku.getText().toString().trim();
		if (null == skuStr || "".equals(skuStr)) {
			Toast.makeText(DataCollectDay.this, "请输入SKU!", Toast.LENGTH_SHORT).show();
			return false;
		}
		dataCollectDayInfo.setBismt(skuStr.toUpperCase());

		String priceStr = txtPrice.getText().toString().trim();
		if (null == priceStr || "".equals(priceStr) || Double.parseDouble(priceStr) == 0) {
			Toast.makeText(DataCollectDay.this, "请输入零售价格或者零售价格不能为0!", Toast.LENGTH_SHORT).show();
			return false;
		}
		dataCollectDayInfo.setSaleprice(priceStr);

		String paiMianStr = txtPaiMian.getText().toString().trim();
		if (null == paiMianStr || "".equals(paiMianStr) || Integer.parseInt(paiMianStr) == 0) {
			Toast.makeText(DataCollectDay.this, "请输入拍面数或者拍面数不能为0!", Toast.LENGTH_SHORT).show();
			return false;
		}
		dataCollectDayInfo.setSide(paiMianStr);

		dataCollectDayInfo.setRemark(txtCommonts.getText().toString().trim());
		return true;
	}

	private View.OnClickListener btnListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.btnSave :
					if (!getData()) {
						return;
					}
					if ("add".equals(status)) {
						for (int i = 0; i < dataCollectDayInfoList.size(); i++) {
							if (dataCollectDayInfo.getBismt().equals(
									dataCollectDayInfoList.get(i).getBismt())) {
								Toast.makeText(DataCollectDay.this, "不能添加重复的SKU!",
										Toast.LENGTH_SHORT).show();
								return;
							}
						}
					} else if ("modify".equals(status)) {
						if (!oldSKU.equals(dataCollectDayInfo.getBismt())) {
							for (int i = 0; i < dataCollectDayInfoList.size(); i++) {
								if (dataCollectDayInfo.getBismt().equals(
										dataCollectDayInfoList.get(i).getBismt())) {
									Toast.makeText(DataCollectDay.this, "不能添加重复的SKU!",
											Toast.LENGTH_SHORT).show();
									return;
								}
							}
						}
					}

					bundle = new Bundle();
					bundle.putSerializable("dataCollectDayInfo", dataCollectDayInfo);
					bundle.putInt("dataCollDayPosition", dataCollDayPosition);
					intent = new Intent();
					intent.putExtras(bundle);
					DataCollectDay.this.setResult(RESULT_OK, intent);
					DataCollectDay.this.finish();
					break;
				case R.id.btnBack :
					DataCollectDay.this.finish();
					break;
			}

		}
	};
}
